package com.hspedu.hspspringmvc.xml;

import jdk.nashorn.internal.ir.CallNode;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.xml.sax.XMLReader;

import java.io.InputStream;

//用于解析spring配置文件
public class XMLParser {
    public static String getBasePackage(String xmlFile){
        SAXReader reader = new SAXReader();
        InputStream inputStream = XMLParser.class.getClassLoader().getResourceAsStream(xmlFile);
        try {
            Document document = reader.read(inputStream);
            Element rootElement = document.getRootElement();
            Element componentScanElement = rootElement.element("component-scan");
            Attribute attribute = componentScanElement.attribute("base-package");
            String basePackage = attribute.getText();
            return basePackage;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }
}
